package priv.hxm.view;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import priv.hxm.speakandlisten.Listen;
import priv.hxm.speakandlisten.Speak;
import priv.mill.net.MessageContexts;
import priv.mill.net.MessageObj;
import priv.mill.net.MyHttp;

public class myMouseListener implements MouseListener {
	private static Logger logger = Logger.getLogger(myMouseListener.class);
	private JTextField sendMsg;
	private JLayeredPane layeredPane;
	private JPanel panel;
	private JPanel panel_1;
	private JTextArea showMsg;
	Listen listen = Listen.getInstance();
	Speak speak = Speak.getInstance();
	private JTextArea my;
	private JTextArea cat;
	private JLabel lblNewLabel;

	public myMouseListener() {// 无参构造
	}

	// 有参构造
	public myMouseListener(JTextField sendMsg, JLayeredPane layeredPane,
			JPanel panel, JPanel panel_1, JTextArea showMsg, JTextArea my,
			JTextArea cat, JLabel lblNewLabel) {
		super();
		this.sendMsg = sendMsg;
		this.layeredPane = layeredPane;
		this.panel = panel;
		this.panel_1 = panel_1;
		this.showMsg = showMsg;
		this.my = my;
		this.cat = cat;
		this.lblNewLabel = lblNewLabel;
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		listen.listen(lblNewLabel);
	}

	/*
	 * 按钮松开
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {

		String text = sendMsg.getText();// 获取消息输入框输入的消息
		sendMsg.setText("");// 重置输入框
		MessageContexts.getInstance().offerMessage(new MessageObj() {

			@Override
			public void handlerMessage(MyHttp info) {
				Object data = info.getData();
				String string = (String) data;
				cat.setText("");
				cat.append("   " + string);
				showMsg.setCaretPosition(showMsg.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
				showMsg.append("虹小猫：" + string + "\n");
				showMsg.setCaretPosition(showMsg.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来

				speak.shuo(string);

			}

			@Override
			public MyHttp getMessage() {// 对不同的用户输入利用工厂获取不同的消息类型
				MyHttp getmyHttp = null;
				try {
					String string;
					Thread.currentThread().sleep(2000);// 线程休眠等待语音输入
					if (listen.finalString == null) {
						string = "我没听清";
					} else {
						string = new String(listen.finalString);
						listen.finalString = null;// 清空上次语音类容
					}

					showMsg.append("我：" + string + "\n");
					showMsg.setCaretPosition(showMsg.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
					logger.debug(string);
					getmyHttp = MyHttpFactory.getmyHttp(string);
					my.setText("");
					my.append("   " + string);
					showMsg.setCaretPosition(showMsg.getDocument().getLength());// 重新定位焦点保证新加的文字能显示出来
					if (getmyHttp.getMyUrl().equals("Plays")) {// 如果是玩游戏，显示游戏界

						// layeredPane.setLayer(panel, 2);
						layeredPane.setLayer(panel_1, 16);// 打开石头剪刀布界面
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				return getmyHttp;
			}
		});
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
